package com.siau.SevenBubble

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import hhm.android.library.LogUtils
import hhm.android.main.linkDataAnalyse
import hhm.android.main.schemeDataAnalyse
import siau.android.base.SBBaseActivity
import siau.android.library.KeyString
import java.io.UnsupportedEncodingException
import java.net.URLDecoder

class AppLinkActivity : SBBaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        //DD:34:FC:B3:32:29:84:94:F5:B7:22:5F:C3:49:4D:F4:91:D1:83:76:7C:BB:52:57:B5:C2:2E:4E:CE:C5:D6:CA
        //com.siau.SevenBubble
        //https://oa.sevenbubble.com/bubble-page/
        //https://oa.sevenbubble.com/bubble-page?param={"pageType":1,"pageId":"324"}&data={"shareUserId":""}

        var data = intent.dataString // 接收到link传过来的数据：https://oa.sevenbubble.com/bubble-page?pageType=1&pageId=1&shareUserId=1  pageType 1文章 2话题 3话题内部帖子 4活动
        if (data?.startsWith("https://oa.sevenbubble.com/bubble-page") == true) {
            data = data.replace("https://oa.sevenbubble.com/bubble-page", "")
            data = URLDecoder.decode(data)
            LogUtils.d("hhm", data)

            //判断登录情况 热启动情况下token能判断是否登陆 冷启动情况下要调用接口判断
            if (!TextUtils.isEmpty(KeyString.token)) {
                schemeDataAnalyse(this, data)
            } else {
                val i = Intent(this, SplashActivity::class.java)
                i.putExtra(KeyString.data, data)
                i.putExtra(KeyString.H5Type, KeyString.scheme)
                i.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
                startActivity(i)
            }
            finish()
        }else if(data?.startsWith("https://oa.sevenbubble.com/bubble-h5") == true){
            data = data.replace("https://oa.sevenbubble.com/bubble-h5", "")
            LogUtils.d("hhm", data)
            //判断登录情况 热启动情况下token能判断是否登陆 冷启动情况下要调用接口判断
            if (!TextUtils.isEmpty(KeyString.token)) {
                linkDataAnalyse(this, data)
            } else {
                val i = Intent(this, SplashActivity::class.java)
                i.putExtra(KeyString.data, data)
                i.putExtra(KeyString.H5Type, KeyString.link)
                i.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
                startActivity(i)
            }
            finish()
        }
    }
}